<template>
  <sweet-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-form" :style="{width: '590px'}">
    <sweet-form-item label="活动名称" prop="name">
      <sweet-validate validate-type="form">
        <sweet-input v-model="ruleForm.name"></sweet-input>
      </sweet-validate>
    </sweet-form-item>
    <sweet-form-item label="活动区域" prop="region">
      <sweet-validate validate-type="form">
        <sweet-select v-model="ruleForm.region" placeholder="请选择活动区域">
          <sweet-option label="区域一" value="shanghai"></sweet-option>
          <sweet-option label="区域二" value="beijing"></sweet-option>
        </sweet-select>
      </sweet-validate>
    </sweet-form-item>
    <sweet-form-item label="活动时间" required>
      <sweet-col :span="11">
        <sweet-form-item prop="date1">
          <sweet-validate validate-type="form">
            <sweet-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1"></sweet-date-picker>
          </sweet-validate>
        </sweet-form-item>
      </sweet-col>
      <sweet-col class="line" :span="2">-</sweet-col>
      <sweet-col :span="11">
        <sweet-form-item prop="date2">
          <sweet-validate validate-type="form">
            <sweet-time-picker placeholder="选择时间" v-model="ruleForm.date2"></sweet-time-picker>
          </sweet-validate>
        </sweet-form-item>
      </sweet-col>
    </sweet-form-item>
    <sweet-form-item label="即时配送" prop="delivery">
      <sweet-switch v-model="ruleForm.delivery"></sweet-switch>
    </sweet-form-item>
    <sweet-form-item label="活动性质" prop="type">
      <sweet-validate validate-type="form">
        <sweet-checkbox-group v-model="ruleForm.type">
          <sweet-checkbox label="美食/餐厅线上活动" name="type"></sweet-checkbox>
          <sweet-checkbox label="地推活动" name="type"></sweet-checkbox>
          <sweet-checkbox label="线下主题活动" name="type"></sweet-checkbox>
          <sweet-checkbox label="单纯品牌曝光" name="type"></sweet-checkbox>
        </sweet-checkbox-group>
      </sweet-validate>
    </sweet-form-item>
    <sweet-form-item label="特殊资源" prop="resource">
      <sweet-validate validate-type="form">
        <sweet-radio-group v-model="ruleForm.resource">
          <sweet-radio label="线上品牌商赞助"></sweet-radio>
          <sweet-radio label="线下场地免费"></sweet-radio>
        </sweet-radio-group>
      </sweet-validate>
    </sweet-form-item>
    <sweet-form-item label="活动形式" prop="desc">
      <sweet-validate validate-type="form">
        <sweet-input type="textarea" v-model="ruleForm.desc"></sweet-input>
      </sweet-validate>
    </sweet-form-item>
    <sweet-form-item>
      <sweet-button type="primary" @click="submitForm('ruleForm')">立即创建</sweet-button>
      <sweet-button @click="resetForm('ruleForm')">重置</sweet-button>
    </sweet-form-item>
  </sweet-form>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        region: '',
        delivery: false,
        type: [],
        resource: '',
        desc: '',
        name: '',
        date1: '',
        date2: '',
      },
      rules: {
        name: [
          {required: true, message: '请输入活动名称', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        region: [{required: true, message: '请选择活动区域', trigger: 'change'}],
        date1: [{type: 'date', required: true, message: '请选择日期', trigger: 'change'}],
        date2: [{type: 'date', required: true, message: '请选择时间', trigger: 'change'}],
        type: [{type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change'}],
        resource: [{required: true, message: '请选择活动资源', trigger: 'change'}],
        desc: [{required: true, message: '请填写活动形式', trigger: 'blur'}]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$alert('submit!');
          return true;
        }
        return false;
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="less" scoped>
.demo-form :deep(.el-form-item__error) {
  display: none;
}
.demo-form {
  .el-select,
  .el-input,
  .el-textarea {
    width: 220px;
  }
  :deep(.el-select) {
    width: 220px;
  }
  .line {
    text-align: center;
  }
}
</style>
